package it.polimi.travelDream.ejb.beans;

import java.util.List;

import it.polimi.travelDream.ejb.Cthulhu.Persistenza;
import it.polimi.travelDream.ejb.interfaces.GiftListMgrRemote;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;


import it.polimi.travelDream.entities.GiftList;


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
/**
 * Session Bean implementation class GiftListMgr
 */
@Stateless
@LocalBean

public class GiftListMgr implements GiftListMgrRemote {

	GiftList giftList;
	Persistenza persist=new Persistenza();
	public EntityManagerFactory factory;
	public EntityManager em;
	
    public GiftList getGiftList() {
		return giftList;
	}

	public void setGiftList(GiftList giftList) {
		this.giftList = giftList;
	}

    public GiftListMgr() {
        // TODO Auto-generated constructor stub
    }

	 /**
	 * @author Filipazzi,Crisciullo
	 * 
	 *This method the User's Gift List
	 */
    
	public List<GiftList> showAllGiftList(int id){
		factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
		em=factory.createEntityManager();

		TypedQuery<GiftList> query = em.createQuery("SELECT gl FROM GiftList gl where gl.idUser="+id+"", GiftList.class);

			List<GiftList> gl = query.getResultList();

			em.close();
			factory.close();
			return gl;
	}
    
	 /**
		 * @author Filipazzi,Crisciullo
		 * 
		 *This method adds a pack to the Gift List
		 */
	@Override
	public GiftList addToGiftList(GiftList gl) {
		// TODO Auto-generated method stub
		persist.persista(gl);
		return gl;
	}

	 /**
		 * @author Filipazzi,Crisciullo
		 * 
		 *This method remove a pack from the Gift List
		 */
	@Override
	public void removeFromGiftList(int id) {
		factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
		em=factory.createEntityManager();
		GiftList gl = em.find(GiftList.class, id);
		em.getTransaction().begin();
		em.remove(gl);
		em.getTransaction().commit();
		em.close();
		factory.close();
	}


	@Override
	public void buyFromGF() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sendToTL() {
		// TODO Auto-generated method stub
		
	}

}
